package com.jingxing.controller;

import com.jingxing.config.ApiResult;
import com.jingxing.config.UserLoginToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/matchInfo")
@Api(value = "API", tags = {"比赛相关"})
@UserLoginToken
public class MatchInfoController {
    @Autowired
    private RestTemplate restTemplate;

    private static final String URL_20001 = "http://JINGXINGPROJECT-SERVER";


    @PostMapping("/getMatchInfos")
    @ApiOperation(value = "查询某个阶段比赛信息")
    public ApiResult getMatchInfos(Integer competitionLevel, String name, String phone) {
        MultiValueMap<String, Object> requestEntity = new LinkedMultiValueMap<>();
        requestEntity.add("competitionLevel", competitionLevel);
        requestEntity.add("name", name);
        requestEntity.add("phone", phone);
        return restTemplate.postForObject(URL_20001 + "/matchInfo/getMatchInfos", requestEntity, ApiResult.class);
    }

    @PostMapping("/lateTimes")
    @ApiOperation("录入选手迟到次数")
    public ApiResult lateTimes(String id, Integer lateFrequency, String updateBy) {
        MultiValueMap<String, Object> requestEntity = new LinkedMultiValueMap<>();
        requestEntity.add("id", id);
        requestEntity.add("lateFrequency", lateFrequency);
        requestEntity.add("updateBy", updateBy);
        return restTemplate.postForObject(URL_20001 + "/matchInfo/lateTimes", requestEntity, ApiResult.class);
    }

    @PostMapping("/score")
    @ApiOperation("选手打分")
    public ApiResult score(String id, Integer rating, String updateBy) {
        MultiValueMap<String, Object> requestEntity = new LinkedMultiValueMap<>();
        requestEntity.add("id", id);
        requestEntity.add("rating", rating);
        requestEntity.add("updateBy", updateBy);
        return restTemplate.postForObject(URL_20001 + "/matchInfo/score", requestEntity, ApiResult.class);
    }

    @PostMapping("/promotionPlayer")
    @ApiOperation("选手晋级/淘汰")
    @Transactional
    public ApiResult promotionPlayer(String id, Integer promotionStatus, String updateBy) {
        MultiValueMap<String, Object> requestEntity = new LinkedMultiValueMap<>();
        requestEntity.add("id", id);
        requestEntity.add("promotionStatus", promotionStatus);
        requestEntity.add("updateBy", updateBy);
        return restTemplate.postForObject(URL_20001 + "/matchInfo/promotionPlayer", requestEntity, ApiResult.class);
    }

    @PostMapping("/promotionPlayers")
    @ApiOperation("选手批量晋级/淘汰")
    @Transactional
    public ApiResult promotionPlayers(List<String> ids, Integer promotionStatus, String updateBy) {
        MultiValueMap<String, Object> requestEntity = new LinkedMultiValueMap<>();
        requestEntity.add("ids", ids);
        requestEntity.add("promotionStatus", promotionStatus);
        requestEntity.add("updateBy", updateBy);
        return restTemplate.postForObject(URL_20001 + "/matchInfo/promotionPlayers", requestEntity, ApiResult.class);
    }


}
